<%@ page import="java.net.URLDecoder" %>
<%@ page import="com.wiscom.is.IdentityFactory" %>
<%@ page import="com.wiscom.is.IdentityManager" %>
<%@ page import="com.apexedu.identity.action.IdLoginAction" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String is_config = request.getRealPath("/client.properties");
    Cookie all_cookies[] = request.getCookies();
    Cookie myCookie;
    String decodedCookieValue = null;
    if (all_cookies != null) {
        for(int i=0; i< all_cookies.length; i++) {
            myCookie = all_cookies[i];
            if( myCookie.getName().equals("iPlanetDirectoryPro") ) {
                decodedCookieValue = URLDecoder.decode(myCookie.getValue(), "GB2312");
            }
        }
    }

    IdentityFactory factory = IdentityFactory.createFactory( is_config );
    IdentityManager im = factory.getIdentityManager();

    String curUser = "";
    if (decodedCookieValue != null ) {
        curUser = im.getCurrentUser( decodedCookieValue );
    }
    if ( curUser.length()==0 ){
        String gotoURL = HttpUtils.getRequestURL(request).toString();
        String loginURL = im.getLoginURL() +"?goto=" + java.net.URLEncoder.encode(gotoURL);
        response.sendRedirect( loginURL );
	}else{
        IdLoginAction.loginByServerSide(curUser, request);
        response.sendRedirect( "/index.jsp" );
//		String gotoURL = HttpUtils.getRequestURL(request).toString();
//		String logoutURL = im.getLogoutURL() +"?goto=" + java.net.URLEncoder.encode(gotoURL);
	}
%>
